package com.sun.module_home.ui.index

import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Search
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.key
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.sun.module_base.ext.click
import com.sun.module_base.model.AppThemeState
import com.sun.module_base.theme.color_666
import com.sun.module_common.model.Article
import com.sun.module_home.model.BannerBean
import com.sun.module_common.widget.ArticleItem

/**
 *@author:sunc
 *@time :2025/8/15 16:26
 *@desc: 描述
 */

@Composable
fun ListTopItem(
    topList: List<Article> = emptyList(),
    bannerList: MutableList<BannerBean> = mutableListOf(),
    appThemeState: MutableState<AppThemeState> = mutableStateOf(AppThemeState()),
    onSearch: () -> Unit = {},
    itemClick: (Article) -> Unit = {}
) {
    Column(Modifier.fillMaxWidth()) {
        if (bannerList.isNotEmpty()) {
            key(appThemeState.value) {// 关键点：以主题色为重组触发条件
                Box(modifier = Modifier.padding(horizontal = 10.dp)) {
                    Row(
                        verticalAlignment = Alignment.CenterVertically,
                        modifier = Modifier
                            .fillMaxWidth()
                            .border(
                                width = 1.dp,
                                color = color_666,
                                shape = RoundedCornerShape(10.dp)
                            )
                            .clip(shape = RoundedCornerShape(10.dp))
                            .click { onSearch.invoke() }
                            .padding(horizontal = 5.dp)
                    ) {
                        Icon(
                            imageVector = Icons.Default.Search,
                            contentDescription = null,
                            tint = MaterialTheme.colorScheme.primary,
                            modifier = Modifier.size(18.dp)
                        )

                        Text(
                            "全网搜索",
                            color = MaterialTheme.colorScheme.primary,
                            fontSize = 12.sp,
                        )
                    }
                }
            }
        }
    }
    if (bannerList.isNotEmpty()) {
        Banner(bannerList)
    }

    topList.forEachIndexed {index, article ->

        key(appThemeState.value) {// 关键点：以主题色为重组触发条件
            ArticleItem(data = article, isTop = true, index = index) {
                itemClick.invoke(article)
            }
        }
    }

}